package net.easyjava.framework.log.aspect;

import net.easyjava.framework.log.LogUtils;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by wenxiaoning on 15/12/19.
 */
@Aspect
@Component
public class ControllerLogAspect {
    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    /**
     * 记录api项目中controller的操作日志
     * @throws Exception
     */
    @Before("execution(* com.*.*.controller.*Controller.*(..))")
    public void beforeApiController() throws Exception{
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();

        LogUtils.logAspectControllerBegin(request);
    }



    /**
     * 拦截controller层所有带有注解的方法
     * @throws Exception
     */
    @AfterReturning("execution(* com.*.*.controller.*Controller.*(..))")
    public void AfterReturning() throws Exception {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        LogUtils.logAspectControllerEnd(request);

    }

}
